package cn.bonoon.kernel.util;


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URLEncoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;

@SuppressWarnings("restriction")
public class ViewUtils {
	/**
	 * 设置下载文件中文件的名称
	 * 
	 * @param filename
	 * @param request
	 * @return
	 */
	public static String encodeFilename(String filename, HttpServletRequest request) {
		/*
		 * 获取客户端浏览器和操作系统信息 在IE浏览器中得到的是：
		 * User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
		 * 在Firefox中得到的是：
		 * User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
		 */
		String agent = request.getHeader("USER-AGENT");
		try {
			if ((agent != null) && (-1 != agent.indexOf("MSIE"))) {
				String newFileName = URLEncoder.encode(filename, "UTF-8");
				newFileName = StringUtils.replace(newFileName, "+", "%20");
				if (newFileName.length() > 150) {
					newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1");
					newFileName = StringUtils.replace(newFileName, " ", "%20");
				}
				return newFileName;
			}
			if ((agent != null) && (-1 != agent.indexOf("Mozilla")))
				return MimeUtility.encodeText(filename, "UTF-8", "B");

			return filename;
		} catch (Exception ex) {
			return filename;
		}
	}

	/**
	 * 压缩图片
	 * 
	 * @param file
	 *            输入图片文件
	 * @param directoryFileName
	 *            目标文件
	 * @param width
	 *            目标宽度
	 * @param height
	 *            目标高度
	 * @param proportion
	 *            等比压缩
	 * @return
	 */
	public static boolean compressImage(File file, String directoryFileName, int width, int height, boolean proportion) {
		boolean ret = false;
		FileOutputStream fileOutputStream = null;
		try {
			if (file == null || directoryFileName == null) {
				return ret;
			}

			fileOutputStream = new FileOutputStream(new File(directoryFileName));
			Image image = ImageIO.read(file);
			if (image.getWidth(null) == -1) {
				return ret;
			}

			int newWidth = 0;
			int newHeight = 0;

			if (image.getWidth(null) > width || image.getHeight(null) > height) {
				if (proportion) {
					int rate1 = image.getWidth(null) / width;
					int rate2 = image.getHeight(null) / height;
					int rate = rate1 > rate2 ? rate1 : rate2;
					newWidth = image.getWidth(null) / rate;
					newHeight = image.getHeight(null) / rate;
				} else {
					newWidth = width;
					newHeight = height;
				}
			} else {
				newWidth = image.getWidth(null);
				newHeight = image.getHeight(null);
			}

			BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

			bufferedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);

			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fileOutputStream);
			encoder.encode(bufferedImage);
			fileOutputStream.close();

			ret = true;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return ret;
	}
}
